﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;
using System.Drawing;

using System.Runtime.Serialization.Formatters.Binary;

public partial class ScanUp : System.Web.UI.Page
{
	public string EnterpriseName = "";
    public string UserID = "";
    public string UserPower = "";
    public string Footer = "";
	
    Common Init = new Common();
    MsSqlHelper mssql = new MsSqlHelper();
    protected void Page_Load(object sender, EventArgs e)
    {
        string corpid = Request.Params["corpid"];
        string typeid = Request["typeid"];
        string endrq = Request["endrq"];
        string ext = Request["Ext"];
        string UploadFile = Request.Params["UploadFile"];

        SaveFile(corpid, UploadFile,endrq, ext, typeid);
    }
    public void SaveFile(string detailid, string UploadFile, string endrq, string ext = ".jpg", string typeid = "", string basePath = "~/Upload/Images/")
    {
        ext = string.IsNullOrEmpty(ext) ? ".jpg" : ext;
        string typename = "";
        switch (typeid)
        {
            case "yyzz":
                typename = "营业执照";
                break;
            case "ypzlgf":
                typename = "经营/生产许可证";
                break;
            case "gspzs":
                typename = "GSP/GMP证书";
                break;
            case "zlbzxy":
                typename = "质量保证协议";
                break;
            case "yzyp":
                typename = "样张样票";
                break;
            case "fpyz":
                typename = "发票样张";
                break;
            case "yinzhang":
                typename = "单位印章";
                break;
            case "frwts":
                typename = "法人委托书";
                break;
            case "bwtrsfz":
                typename = "被委托人身份证";
                break;
            case "khxkz":
                typename = "开户许可证";
                break;
            case "zlbznldcb":
                typename = "质量保证能力调查表";
                break;
            case "dwbgjl":
                typename = "单位变更记录";
                break;
            case "kpxx":
                typename = "开票信息";
                break;
            case "dbthxkpj":
                typename = "蛋白同化制剂、肽类激素许可批件";
                break;
            case "yljgzyxkz":
                typename = "医疗机构执业许可证";
                break;
            case "ylqxjyxkz":
                typename = "医疗器械经营许可证";
                break;
            case "spltxkz":
                typename = "食品经营许可证";
                break;
            case "qyndgsb":
                typename = "企业年度公示表";
                break;
            case "swdlz":
                typename = "税务登记证";
                break;
            case "hetong":
                typename = "合同";
                break;
            default:
                typename = typeid;
                break;
        }
        string fullbasePath = (basePath.IndexOf("~") > -1) ? HttpContext.Current.Server.MapPath(basePath) : fullbasePath = basePath;
        //string folderdt = DateTime.Now.ToShortDateString().ToString();
        string folderdt = DateTime.Now.ToString("yyyy'/'MM'/'dd");
        if (!Directory.Exists(fullbasePath))
            Directory.CreateDirectory(fullbasePath);
        if (!Directory.Exists(basePath + folderdt + "\\"))
            Directory.CreateDirectory(fullbasePath + folderdt + "\\");
        string f = "00000000";
        f = Init.maxbh("SCA").ToString();
        string full = fullbasePath + folderdt + "\\" + f + ext;
        string sql = "insert into [ImageDoc](imageid,type_id,leibie,detailid,extend,beactive,url,fzxx1,fzxx5,erq,lastmodifytime)values('" + f + "',rtrim('" + typeid + "'),'" + typename + "',rtrim('" + detailid + "'),'" + ext + "','是','" + basePath.Replace("~", "") + folderdt + "/" + f + ext + "','"+endrq+"','','"+endrq+"',CONVERT(varchar(100), GETDATE(), 120))";
        int success = mssql.ExecuteSql(sql);
        /*
        if (!string.IsNullOrEmpty(UploadFile))
        {
            FileStream fs = new FileStream(full, FileMode.Create, FileAccess.Write);
            fs.Write(Convert.FromBase64String(UploadFile), 0, Convert.FromBase64String(UploadFile).Length);
            fs.Flush();
            fs.Close();
        }
         */

      // Bitmap images=new Bitmap("");
        System.Drawing.Image images = ToImage(UploadFile);
       images.Save(full);
    }

    public System.Drawing.Image ToImage(string base64)
    {
        byte[] bytes = Convert.FromBase64String(base64);
        MemoryStream memStream = new MemoryStream(bytes);
        BinaryFormatter binFormatter = new BinaryFormatter();
        System.Drawing.Image img = (System.Drawing.Image)binFormatter.Deserialize(memStream);
        return img;
    }
}